﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using tools;
using info;

public class ZJHMenu : MonoBehaviour {

	// Use this for initialization
	private GameObject Btn = null;
	private GameObject ReadyBtn = null;
	void Start () {
		Btn = transform.GetChild (0).gameObject;
		EventTriggerListener.Get (transform.GetChild (0).gameObject).onClick = CloseRoom;
		EventTriggerListener.Get (transform.GetChild (1).gameObject).onClick = TakeFriends;
		EventTriggerListener.Get (transform.GetChild (2).gameObject).onClick = Ready;
		ReadyBtn = transform.GetChild (2).gameObject;
	}

	private void CloseRoom(object go){
		if (info.UserInfo.room.owner == info.UserInfo.UserData.id) {
			GameObject.Find ("LeaveRoomAlert").GetComponent<ZJHLeaveRoomAlert> ().ShowView ();
		} else {
			GSocket.Send (ByteFactory.makeByte (621117, null));
		}
	}

	public void HideCloseRoom(){
		Btn.SetActive (false);
	}

	private void TakeFriends(object go){
		GameObject.Find("Main_ca").GetComponent<Demo> ().doWeiChatShareText4IOS ();
	}

	private void Ready(object go){
		GSocket.Send (ByteFactory.makeByte (621107, null));
	}

	public void OpenView(object data){
		transform.localPosition = Vector3.zero;
	}

	public void HideView(object data){
		transform.localPosition = new Vector3(10000,0,0);
	}
	
	// Update is called once per frame
	int i = 0;
	int s = 15;
	void Update () {
		i++;
		if( i > s ){

			for( int q = 0 ;  q < UserInfo.room.seats.Count ; q++ ){
				if( UserInfo.room.seats[q].playerId == UserInfo.UserData.id ){
					ReadyBtn.SetActive (UserInfo.room.seats [q].ready != 0 ? false : true);
				}
			}

			if (ZjhView.Igame) {
				HideView (null);
			} else {
				OpenView (null);
			}

			if ( ZjhView.PlayTurnNum > 0 ) {
				HideCloseRoom ();
			}


			/*
			for( int p = 0 ; p < info.UserInfo.room.seats.Count; p++ ){
				if( info.UserInfo.room.seats[p].playerId == info.UserInfo.UserData.id ){

					if (info.UserInfo.room.seats [p].ready == 1) {
						HideView (null);
					} else {
						OpenView (null);
					}
		
				}
			}
			*/
		}
	}
}
